//
//  FavorView.swift
//  whale
//
//  Created by topnet on 2022/7/6.
//

import UIKit

class FavorView: ICBaseView {

    override func _initUI() {
        
    }
    
    func configDown(down strs: [String]) {
        itemsCount = strs.count
        for i in 0..<strs.count {
            let itemV = getItemV(index: i)
            itemV.downLbl.text = strs[i]
        }
        initFrame()
    }

    func configTop(top strs: [String]) {
        itemsCount = strs.count
        for i in 0..<strs.count {
            let itemV = getItemV(index: i)
            itemV.topLbl.text = strs[i]
        }
        initFrame()
    }
    
    override func initFrame() {
        let itemW = (bounds.width - CGFloat(itemsCount - 1))/CGFloat(itemsCount)
        var lastMaxX: CGFloat = 0
        subviews.forEach { v in
            if v is UpDownLabel {
                v.left(lastMaxX).top(0).bottom(bounds.height).width(itemW)
            }else {
                v.left(lastMaxX).centerY(bounds.height/2).width(1).height(15.adapter)
            }
            lastMaxX = v.frame.maxX
        }
        super.initFrame()
    }
    
    private func getItemV(index: Int)-> UpDownLabel {
        let itemV = (viewWithTag(itemTag+index) as? UpDownLabel) ?? UpDownLabel()
        itemV.tag = itemTag + index
        itemV.tapClosure = { [weak self] in
            self?.didSelect?(index)
        }
        if itemV.superview == nil {
            addSubview(itemV)
        }
        if topFont != nil {
            itemV.topLbl.font = topFont
        }
        if topColor != nil {
            itemV.topLbl.textColor = topColor
        }
        if downFont != nil {
            itemV.downLbl.font = downFont
        }
        if downColor != nil {
            itemV.downLbl.textColor = downColor
        }
        if index < itemsCount - 1 {
            let sepratorLine = viewWithTag(sepratorTag + index) ?? UIView()
            sepratorLine.backgroundColor = .separator
            sepratorLine.tag = sepratorTag + index
            if sepratorLine.superview == nil {
                addSubview(sepratorLine)
            }
        }
        return itemV
    }
    
    var topFont: UIFont?
    var topColor: UIColor?
    var downFont: UIFont?
    var downColor: UIColor?
    var didSelect: IndexClosure?
    
    private let itemTag = 200
    private let sepratorTag = 300
    private var itemsCount = 0
}

private class UpDownLabel: ICBaseView {
    
    override func _initUI() {
        topLbl = UILabel()
        topLbl.textColor = .selectedBackground
        topLbl.font = .boldSystemFont(ofSize: 22)
        topLbl.textAlignment = .center
        
        downLbl = UILabel()
        downLbl.textColor = .main
        downLbl.font = .systemFont(ofSize: 15)
        downLbl.textAlignment = .center
        
        da.addSubviews(sub: [topLbl, downLbl])
        
        let tap = UITapGestureRecognizer(target: self, action: #selector(actionTap))
        tap.numberOfTapsRequired = 1
        addGestureRecognizer(tap)
    }
    
    @objc private func actionTap() {
        tapClosure?()
    }
    
    override func initFrame() {
        topLbl.left_right(self).top(0).bottom((bounds.height-spacing)*0.6)
        downLbl.left_right_offsetTop(topLbl, x: spacing).bottom(bounds.height)
        super.initFrame()
    }
    
    var spacing: CGFloat = 10.adapter
    var topLbl: UILabel!
    var downLbl: UILabel!
    var tapClosure: (()->())?
    
}
